﻿import "../../Chart.js/chart.umd.min.js";

Chart.defaults.font.size = 16;

export function createChart(domCanvas) {
    if (domCanvas == null)
        return null;
    let chartConfig = {
        type: 'line',
        data: {
            labels: [0, 360],
            datasets: [{
                label: 'X',
                data: [NaN, NaN],
                borderWidth: 1,
                borderColor: '#00A',
                backgroundColor: '#00A'
            }, {
                label: 'Y',
                data: [NaN, NaN],
                borderWidth: 1,
                borderColor: '#0A0',
                backgroundColor: '#0A0'
            }, {
                label: 'Z',
                data: [NaN, NaN],
                borderWidth: 1,
                borderColor: '#A00',
                backgroundColor: '#A00'
            }]
        },
        options: {
            maintainAspectRatio: false,
            interaction: {
                // Overrides the global setting
                mode: 'index',
                intersect: false
            },
            scales: {
                x: {
                    beginAtZero: true,
                    min: 0,
                    max: 360
                },
                y: {
                    beginAtZero: true
                }
            }
        }
    };
    let chart = new Chart(domCanvas, chartConfig);
    domCanvas.chart = chart;
    //console.log(chart);
    return chart;
}

export function setChartData(domCanvas, ts, xs, ys, zs) {
    let chart = domCanvas?.chart;
    if (chart == null)
        return;
    //console.log('js.a');
    //console.log(xs);
    chart.data.labels = ts;
    chart.data.datasets[0].data = xs;
    chart.data.datasets[1].data = ys;
    chart.data.datasets[2].data = zs;
    //console.log('js.b');
}

export function setChartVRange(domCanvas, vmin, vmax) {
    let chart = domCanvas?.chart;
    if (chart == null)
        return;
    //console.log('a');
    //console.log(vmin);
    //console.log(vmax);
    chart.options.scales.y.min = vmin;
    chart.options.scales.y.max = vmax;
}
